package com.xsimas.xyybackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsimas.xyybackend.domain.FormSubmission;
import com.xsimas.xyybackend.dto.PageRequestDTO;
import com.xsimas.xyybackend.pojo.Result;
import com.xsimas.xyybackend.service.FormSubmissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RAY_HUI
 * @date 2025-07-09
 * @time 18:55
 */

@RestController
@RequestMapping("/api/form")
@Tag(name = "表单数据管理接口", description = "提供通用表单数据的增删查改及分页操作")
public class FormController {

    @Autowired
    private FormSubmissionService formManageService;

    /**
     * 分页查询表单数据
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询表单数据", description = "根据页码和每页数量分页查询表单记录")
    public Result<IPage<FormSubmission>> getFormSubmissionByPage(@RequestBody PageRequestDTO pageRequest) {
        Page<FormSubmission> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.ok(formManageService.page(page));
    }

    /**
     * 查询所有表单数据（无分页）
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有表单数据列表", description = "返回所有表单记录")
    public Result<List<FormSubmission>> getAllFormSubmission() {
        return Result.ok(formManageService.list());
    }

    /**
     * 根据ID查询表单数据
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单个表单数据", description = "通过ID查询表单详情")
    public Result<FormSubmission> getFormSubmissionById(
            @Parameter(description = "表单数据ID", required = true)
            @PathVariable Long id) {
        return Result.ok(formManageService.getById(id));
    }

    /**
     * 新增一条表单数据
     */
    @PostMapping("/add")
    @Operation(summary = "新增一条表单数据", description = "将传入的数据对象保存到数据库")
    public Result<Boolean> createFormSubmission(
            @Parameter(description = "表单数据实体对象", required = true)
            @RequestBody FormSubmission data) {
        boolean success = formManageService.save(data);
        return success ? Result.ok(success) : Result.error(400, "新增表单数据失败");
    }

    /**
     * 更新一条表单数据
     */
    @PutMapping("/update")
    @Operation(summary = "更新一条表单数据", description = "根据ID更新表单信息")
    public Result<Boolean> updateFormSubmission(
            @Parameter(description = "表单数据实体对象", required = true)
            @RequestBody FormSubmission data) {
        QueryWrapper<FormSubmission> formSubmissionQueryWrapper = new QueryWrapper<>();
        boolean success = formManageService.update(formSubmissionQueryWrapper);
        return success ? Result.ok(success) : Result.error(400, "更新表单数据失败");
    }

    /**
     * 删除一条表单数据
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除一条表单数据", description = "逻辑或物理删除指定ID的表单数据")
    public Result<Boolean> deleteFormSubmission(
            @Parameter(description = "表单数据ID", required = true)
            @PathVariable Long id) {
        boolean success = formManageService.removeById(id);
        return success ? Result.ok(success) : Result.error(400, "删除表单数据失败");
    }
}